package com.cg.controller;

import com.cg.dto.*;
import com.cg.entity.DmUser;
import com.cg.service.DmUserService;
import com.cg.vo.LoginUserVo;
import com.cg.vo.TokenVO;
import com.cg.vo.UpdateUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户表(DmUser)表控制层
 *
 * @author makejava
 * @since 2023-11-06 12:02:35
 */
@RestController
@RequestMapping("user")
public class DmUserController {
    /**
     * 服务对象
     */
    @Resource
    private DmUserService dmUserService;

    //用户登录
    @PostMapping("/login")
    public Dto<Object[]> login(@RequestBody LoginDto loginDto) {
        return dmUserService.login(loginDto);
    }


    //根据token获取用户信息
    @PostMapping("/loadCurrentUserByToken")
    //public Dto<DmUser> findUserByToken(HttpServletRequest request) {
    public Dto<DmUser> findUserByToken(@RequestParam("token") String token) {
        //String token = request.getHeader("token");
        return dmUserService.findUserByToken(token);
    }


    //刷新Token
    @PostMapping("/replaceToken")
    public Dto<TokenVO> replaceToken(@RequestParam("token") String token) {
        return dmUserService.replaceToken(token);
    }

    //获取注册验证码接口
    @PostMapping("/code")
    public Dto sendSms(@RequestBody UserRegDto userRegDto) {
        return dmUserService.sendSms(userRegDto.getPhone());
    }

    @PostMapping("/checkEmail")
    public Dto checkEmail(@RequestBody UserRegDto userRegDto){
        return dmUserService.checkEmail(userRegDto.getPhone());
    }

    //新用户注册
    @PostMapping("/register")
    public Dto register(@RequestBody RegDto regDto) {
        return dmUserService.register(regDto);
    }



    //个人信息获取
    //根据token获取个人信息
    @PostMapping("/queryUserInfoByToken")
    public Dto<LoginUserVo> queryUserInfoByToken(@RequestParam("token") String token) {
        return DtoUtil.returnDataSuccess(dmUserService.queryUserInfoByToken(token));
    }


    //用户信息修改---无头像修改
    @PostMapping("/modifyUserInfo")
    public Dto updateUser(@RequestBody UpdateUserVo updateUserVo) {
        DmUser dmUser = new DmUser();
        BeanUtils.copyProperties(updateUserVo,dmUser);
        dmUser.setId(updateUserVo.getUserId());
        dmUserService.updateUser(updateUserVo.getToken(),dmUser);
        return DtoUtil.returnSuccess();
    }
}

